package com.example.exchanges_mode.listening;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;



/**
 * 监听我们的交换机
 */
@Slf4j
@Component
public class Listening {

    /**
     * 测试高级用法
     * LazyQueue:将数据直接存到磁盘中
     * durable: 持久化
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "lazyqueue.topic",
                    durable = "true",
                    arguments = @Argument(name = "x-queue-mode", value = "lazy")
            ),
            exchange = @Exchange(name = "lazy.topic", type = ExchangeTypes.TOPIC),
            key = "lazy"
    ))
    public void LazyQueueTest(String message){
        /**
         * none:不处理
         * manual:手动模式
         * auto:自动模式 :1.如果是业务异常(运行异常(最大的异常))，会自动返回nack
         *              2.如果是消息异常或校验异常，自动返回reject
         * spring: 开启消费者的重试机制:(会重试完成之后，删除掉)
         */
        log.info("接受交换机的消息:{}",message);
//        //运行异常
//        throw new RuntimeException();
         //消息异常
//        throw new MessageConversionException("故意的");

    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue", durable = "true"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.TOPIC),
            key = "delay"
    ))
//    @RabbitListener(queues = "direct.queue")
    public void listenDelayMessage(String msg){
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }
}
